#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化版的豆包音乐下载器GUI，只保留核心界面部分
"""

import sys
import os

print("开始启动简化版GUI...")

# 只导入必要的PyQt5组件
try:
    from PyQt5.QtWidgets import (
        QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, 
        QLabel, QLineEdit, QPushButton, QProgressBar, QTextEdit, 
        QCheckBox, QMessageBox, QFileDialog, QGroupBox, QFormLayout
    )
    from PyQt5.QtCore import Qt, QThread, pyqtSignal
    from PyQt5.QtGui import QFont, QTextOption
    print("✅ PyQt5组件导入成功")
except Exception as e:
    print(f"❌ PyQt5导入失败: {e}")
    sys.exit(1)

# 简单的日志类
class SimpleLogger:
    def info(self, message):
        print(f"[INFO] {message}")
    def error(self, message):
        print(f"[ERROR] {message}")
    def warning(self, message):
        print(f"[WARNING] {message}")

global_logger = SimpleLogger()

# 模拟Selenium可用性
selenium_available = False

class MusicDownloaderGUI(QMainWindow):
    """简化版豆包音乐下载器图形界面类"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("豆包音乐下载器 v1.4")
        self.setGeometry(100, 100, 800, 600)
        self.download_thread = None
        self.init_ui()
    
    def init_ui(self):
        """初始化用户界面"""
        # 设置主布局
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        main_layout = QVBoxLayout(central_widget)
        
        # 创建输入区域
        input_group = QGroupBox("输入信息")
        input_layout = QFormLayout()
        
        # 分享链接输入框
        self.url_input = QLineEdit()
        self.url_input.setPlaceholderText("请输入豆包音乐分享链接")
        self.url_input.setMinimumHeight(30)
        input_layout.addRow("分享链接:", self.url_input)
        
        # 保存路径选择
        path_layout = QHBoxLayout()
        self.path_input = QLineEdit()
        self.path_input.setPlaceholderText("选择保存目录")
        self.path_input.setMinimumHeight(30)
        self.path_input.setText(os.path.join(os.getcwd(), 'downloads'))
        
        browse_button = QPushButton("浏览...")
        browse_button.clicked.connect(self.on_browse_clicked)
        browse_button.setMinimumHeight(30)
        
        path_layout.addWidget(self.path_input)
        path_layout.addWidget(browse_button)
        input_layout.addRow("保存目录:", path_layout)
        
        # 选项设置
        options_layout = QHBoxLayout()
        
        # 使用Selenium模式复选框
        self.selenium_checkbox = QCheckBox("使用Selenium模式")
        if not selenium_available:
            self.selenium_checkbox.setChecked(False)
            self.selenium_checkbox.setEnabled(False)
            self.selenium_checkbox.setToolTip("Selenium模块未安装")
        
        options_layout.addWidget(self.selenium_checkbox)
        options_layout.addStretch()
        input_layout.addRow(options_layout)
        
        # 添加输入区域到主布局
        input_group.setLayout(input_layout)
        main_layout.addWidget(input_group)
        
        # 创建控制按钮区域
        control_group = QGroupBox("操作")
        control_layout = QHBoxLayout()
        
        # 解析按钮
        self.parse_button = QPushButton("📋 解析链接")
        self.parse_button.setMinimumHeight(35)
        control_layout.addWidget(self.parse_button)
        
        # 下载按钮
        self.download_button = QPushButton("⬇️ 下载音乐")
        self.download_button.setMinimumHeight(35)
        self.download_button.setEnabled(False)  # 初始禁用
        control_layout.addWidget(self.download_button)
        
        # 清理按钮
        self.clear_button = QPushButton("🧹 清理")
        self.clear_button.setMinimumHeight(35)
        control_layout.addWidget(self.clear_button)
        
        # 关于按钮
        self.about_button = QPushButton("❓ 关于")
        self.about_button.setMinimumHeight(35)
        control_layout.addWidget(self.about_button)
        
        # 添加控制区域到主布局
        control_group.setLayout(control_layout)
        main_layout.addWidget(control_group)
        
        # 创建音乐信息区域
        info_group = QGroupBox("音乐信息")
        info_layout = QFormLayout()
        
        # 音乐信息输入框
        self.title_input = QLineEdit()
        self.title_input.setReadOnly(True)
        self.title_input.setMinimumHeight(30)
        info_layout.addRow("歌曲标题:", self.title_input)
        
        self.artist_input = QLineEdit()
        self.artist_input.setReadOnly(True)
        self.artist_input.setMinimumHeight(30)
        info_layout.addRow("艺术家:", self.artist_input)
        
        self.duration_input = QLineEdit()
        self.duration_input.setReadOnly(True)
        self.duration_input.setMinimumHeight(30)
        info_layout.addRow("时长:", self.duration_input)
        
        self.size_input = QLineEdit()
        self.size_input.setReadOnly(True)
        self.size_input.setMinimumHeight(30)
        info_layout.addRow("文件大小:", self.size_input)
        
        # 添加音乐信息区域到主布局
        info_group.setLayout(info_layout)
        main_layout.addWidget(info_group)
        
        # 创建进度区域
        progress_group = QGroupBox("下载进度")
        progress_layout = QVBoxLayout()
        
        # 进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setValue(0)
        self.progress_bar.setTextVisible(True)
        self.progress_bar.setMinimumHeight(25)
        progress_layout.addWidget(self.progress_bar)
        
        # 状态信息
        status_layout = QHBoxLayout()
        
        # 状态标签
        self.status_label = QLabel("就绪")
        status_layout.addWidget(self.status_label)
        
        # 下载速度
        self.speed_label = QLabel("速度: --")
        status_layout.addWidget(self.speed_label)
        
        # 预计时间
        self.time_label = QLabel("剩余: --")
        status_layout.addWidget(self.time_label)
        
        status_layout.addStretch()
        progress_layout.addLayout(status_layout)
        
        # 添加进度区域到主布局
        progress_group.setLayout(progress_layout)
        main_layout.addWidget(progress_group)
        
        # 创建日志区域
        log_group = QGroupBox("日志信息")
        log_layout = QVBoxLayout()
        
        # 日志文本框
        self.log_text = QTextEdit()
        self.log_text.setReadOnly(True)
        self.log_text.setLineWrapMode(QTextEdit.WidgetWidth)
        self.log_text.setWordWrapMode(QTextOption.WrapAtWordBoundaryOrAnywhere)
        log_layout.addWidget(self.log_text)
        
        # 添加日志区域到主布局
        log_group.setLayout(log_layout)
        main_layout.addWidget(log_group)
        
        # 添加状态栏
        self.statusBar().showMessage("就绪")
        
        # 添加事件处理
        self.parse_button.clicked.connect(self.on_parse_clicked)
        self.download_button.clicked.connect(self.on_download_clicked)
        self.clear_button.clicked.connect(self.on_clear_clicked)
        self.about_button.clicked.connect(self.on_about_clicked)
        self.url_input.returnPressed.connect(self.on_parse_clicked)
    
    def on_browse_clicked(self):
        """浏览文件夹"""
        directory = QFileDialog.getExistingDirectory(self, "选择保存目录")
        if directory:
            self.path_input.setText(directory)
    
    def on_parse_clicked(self):
        """解析链接"""
        self.status_label.setText("正在解析...")
        self.statusBar().showMessage("正在解析链接")
        
        # 模拟解析成功
        self.title_input.setText("测试歌曲")
        self.artist_input.setText("测试艺术家")
        self.duration_input.setText("03:45")
        self.size_input.setText("7.5 MB")
        
        self.download_button.setEnabled(True)
        self.status_label.setText("解析成功")
        self.statusBar().showMessage("解析完成")
    
    def on_download_clicked(self):
        """下载音乐"""
        self.status_label.setText("正在下载...")
        self.statusBar().showMessage("开始下载")
        
        # 模拟下载进度
        import time
        for i in range(101):
            self.progress_bar.setValue(i)
            time.sleep(0.01)
        
        self.status_label.setText("下载完成")
        self.statusBar().showMessage("下载成功")
        QMessageBox.information(self, "成功", "音乐下载成功！")
    
    def on_clear_clicked(self):
        """清理输入和信息"""
        # 清除输入
        self.url_input.clear()
        
        # 清除音乐信息
        self.title_input.clear()
        self.artist_input.clear()
        self.duration_input.clear()
        self.size_input.clear()
        
        # 重置进度
        self.progress_bar.setValue(0)
        self.status_label.setText("就绪")
        self.speed_label.setText("速度: --")
        self.time_label.setText("剩余: --")
        
        # 重置状态栏
        self.statusBar().showMessage("就绪")
        
        # 禁用下载按钮
        self.download_button.setEnabled(False)
    
    def on_about_clicked(self):
        """显示关于对话框"""
        QMessageBox.about(self, "关于", "豆包音乐下载器 v1.4\n\n用于下载豆包音乐分享链接中的音乐\n\n支持普通模式和Selenium浏览器自动化模式\n\n作者: AI Assistant")

if __name__ == "__main__":
    """程序主入口"""
    print("正在创建QApplication...")
    app = QApplication(sys.argv)
    print("设置应用程序样式...")
    app.setStyle("Fusion")
    
    print("创建主窗口...")
    window = MusicDownloaderGUI()
    print("显示窗口...")
    window.show()
    
    print("启动应用程序主循环...")
    sys.exit(app.exec_())
